package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.AttentionVO;
import com.cloudeasy.user.po.Attention;
import com.cloudeasy.user.po.UserInfo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 关注工具类
 */
public class AttentionUtil {

    /**
     * 转换VO对象
     *
     * @param userInfo {@link UserInfo}
     * @return {@link AttentionVO}
     */
    public static AttentionVO convert(UserInfo userInfo) {
        AttentionVO attentionVO = new AttentionVO();
        BeanUtils.copyProperties(userInfo, attentionVO);
        return attentionVO;
    }

    /**
     * 转换VO对象
     *
     * @param userInfos {@link List<UserInfo>}
     * @return {@link List<AttentionVO>}
     */
    public static List<AttentionVO> convert(List<UserInfo> userInfos) {
        if (userInfos == null) {
            return new ArrayList<>();
        }
        return userInfos.stream().map(AttentionUtil::convert).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param userInfos {@link List<UserInfo>}
     * @return {@link PageResult<AttentionVO>}
     */
    public static PageResult<AttentionVO> convert(IPage<Attention> attentions, List<UserInfo> userInfos) {
        PageResult<AttentionVO> attentionVOPageResult = new PageResult<>();
        attentionVOPageResult.setTotal(attentions.getTotal())
                .setPages(attentions.getPages())
                .setRecords(userInfos.stream().map(AttentionUtil::convert).collect(Collectors.toList()));
        return attentionVOPageResult;
    }
}
